import api from '@/api'
import { deepCopy, setPage } from '@/utils/util'

const initPage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 1,
    filter: {
        enabled: null,
        classIds: null,
        name: null
    }
}

const state = {
    data: {
        studyList: [],
        businessTree: [],
        businessTemplet: []
    },
    ui: {
        page: deepCopy(initPage)
    }
}

// getters    
const getters = {
    studyList: state => state.data.studyList,
    businessTree: state => state.data.businessTree,
    businessTemplet: state => state.data.businessTemplet
}

// actions
const actions = {

    getstudyClasses({ commit }, data) {
        api.getstudyTask(data ? data : state.ui.page, {
            loading: true,
            success: res => {
                const { pages, total } = res
                commit('SETSTUDYPAGE', { pages, total })
                commit('SETSTUDYLIST', res)
            }
        })
    },

    // 删除某一条数据
    removeonedata({ dispatch }, data) {
        api.removeone(
            data, {
                success: () => {
                    dispatch('getstudyClasses')
                }
            }
        )
    },

    // 添加任务
    addTask({ dispatch }, objc = {}) {
        api.createTask(
            objc.data, {
                success: (res) => {
                    dispatch('getstudyClasses')
                    objc.cb && objc.cb(res)
                }

            }
        )
    },

    // 修改任务
    editTask({ dispatch }, data) {
        api.editTask(
            data, {
                success: (res) => {
                    dispatch('getstudyClasses')
                }
            }
        )
    },

    // 获取业务详情
    getTaskDetail({ commit }, data) {
        api.getTaskDetail({
            data: data,
            success: (res) => {
                console.log(res)
            }
        })
    },

    // 根据ID获取任务列表
    queryFindTask({ commit }, type, page) {
        api.queryOneTask(
            type ? type : '',
            page ? page : state.ui.page, {
                loading: true,
                success: res => {
                    const { pages, total } = res
                    commit('SETSTUDYPAGE', { pages, total })
                    commit('SETSTUDYLIST', res)
                }
            })
    },

    // addTaskRelation
    addTaskRelation({ commit }, params) {
        api.addTaskRelation(params, {
            success: (res) => {
                console.log(res)
            }
        })
    },

    // 获取业务分类TreeData
    getBusinessTreeData({ commit }, params) {
        console.log(api)
        api.getClassifications('key_class_business', {
            success: businessTree => {
                commit('SETBUSINESSTREEDATA', { businessTree })
            }
        })
    },

    // 根据业务分类ID获取对应业务列表
    getBusinessList({ commit }, params) {
        api.getBusinesses(params, {
            loading: true,
            success: res => {
                let businessTemplet = res.content ? res.content : []
                commit('SETBUSINESSTREETEMPLET', { businessTemplet })
            }
        })
    }
}

// mutations
const mutations = {

    SETSTUDYLIST: (state, payload) => {
        state.data.studyList = payload;
    },

    SETBUSINESSTREEDATA(state, payload) {
        state.data.businessTree = payload.businessTree
    },

    SETBUSINESSTREETEMPLET(state, payload) {
        state.data.businessTemplet = payload.businessTemplet
    },

    SETSTUDYPAGE(state, page) {
        page && setPage(state.ui.page, page)
    }

}

export default {
    state,
    getters,
    actions,
    mutations
}